$(function () {
    initValue();
    getData();
    $("body").on("click", "#delete", function () {
        var _this = $(this);
        var value = _this.data('value');
        var overallValue = _getLOCALSTORAGE("overallKey");
        var index = overallValue.indexOf("" + value + "");
        if (index > -1) {
            overallValue.splice(index, 1);
        }
        _setLOCALSTORAGE("overallKey", overallValue);
        $("#" + value).remove();
        getData();
    }).on('click','.go-to-exam',function () {//参加考试
        var em = $(this).parents('[data-exam]');
        var examId = $(em).data('exam');
        $.ajax({
            url: baseloaction +'/web/uc/exam/record/goToExam',
            type:"post",
            dataType:'json',
            data:{
                'examId':examId
            },
            async: false,
            success:function (result) {
                if(result.success==false){
                    icModale(0,'',result.message);
                }else{
                    window.open(baseloaction + '/web/uc/exam/record/startExamination?examRecordId='+result.entity);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，参加考试失败');
            }
        });
    }).on('click','.free-exam-box',function () {//免费报名
        var _this = $(this);
        var examId = $(this).data('id');
        var payType = _this.data('pay-type');
        isLogin(function () {
            if(checkRegTime(_this)){
                //生成免费报名记录
                genFreeOrder(payType, examId + '-EXAM-1', function (result) {
                    _this.find("button").text("参加考试");
                    _this.removeClass("free-exam-box");
                    _this.addClass("go-to-exam");
                    _this.find("button").addClass("btn-warning").removeClass('btn-success');
                });
            }
        });
    }).on('click','.a-s-c-d',function () {//添加购买车
        var shopId = $(this).data('s-id');
        var shopType = $(this).data('type');
        addShopCart(shopId,shopType,1);
    }).on('click','.buy-exam-box',function () {
        var _this = $(this);
        var shopId = _this.data('s-id');
        var shopType = _this.data('type');
        isLogin(function () {
            if(checkRegTime(_this)){
                //购买
                window.location.href = baseloaction +'/web/uc/order/initPayOrder?orderType=COMMON&shopData='+shopId+'-'+shopType+'-1&shareKey=';
            }
        });
    });
});

//检查试卷报名时间
function checkRegTime(_this){
    var beginTime = _this.parents(".exam-pay-list").find("i.fa-clock-o").text();
    var flag =true;
    if(beginTime){
        beginTime = beginTime.replace(/—/g,"/").split('/');
        var date = new Date(beginTime[1]);
        if($.now() > date){
            icModale(0,'','考试报名时间已过，请报名其他考试');
            flag =false;
        }
    }
    return flag;
}


function getData() {
    getDataList("course");
    getDataList("teacher");
    getDataList("examPaper");
    getDataList("article");
}

function initValue() {
    var overallValue = _getLOCALSTORAGE("overallKey");
    var searchValue = $("#searchValue").val();
    if (isEmpty(overallValue)) {
        overallValue = searchValue.split(',');
    } else {
        var index = overallValue.indexOf("" + searchValue + "");
        if (index <= -1 && !isEmpty(searchValue)) {
            overallValue.push(searchValue);
            _setLOCALSTORAGE("overallKey", overallValue);
        }
    }
    var html = '';
    overallValue.forEach((item, index, array) => {
        html += ' <span class="label" id=' + item + '>\n' +
            '' + item + '\n' +
            '<button class="close" type="button" title="关闭" id="delete" data-value=' + item + '>×</button>\n' +
            '</span>';
    });
    _setLOCALSTORAGE("overallKey", overallValue);
    $(".values").html(html);
}

function getDataList(type) {
    var overallValue = _getLOCALSTORAGE("overallKey");
    if (isEmpty(overallValue)) {
        overallValue = "";
    } else {
        overallValue = overallValue.join(',')
    }
    var url = '';
    if ("course" == type) {
        url = baseloaction + '/web/globalSearch/globalSearchCourse';
    } else if ("teacher" == type) {
        url = baseloaction + '/web/globalSearch/globalSearchTeacher';
    } else if ("examPaper" == type) {
        url = baseloaction + '/web/globalSearch/globalSearchExamPaper';
    } else {
        url = baseloaction + '/web/globalSearch/globalSearchArticle';
    }
    $.ajax({
        url: url,
        data: {'overallValue': overallValue},
        type: 'post',
        dataType: "text",
        success: function (result) {
            $("#" + type + "Context").html(result);
            if ("course" == type) {
                lazyCourseImage();
            } else if ("teacher" == type) {
                lazyTeacherImage()
            }
        },
        error: function (error) {
            var _html = '<section class="no-data__wrap"><span class="no-data-ico"></span><p class="mt20"><span class="c-666">没有搜索到相关数据</span></p></section>'
            $("#" + type + "context").html(_html);
        }
    })
}

var isEmpty = function (str) {
    if (str === null || typeof str === 'undefined' || str === '') {
        return true
    }
    return false
}
